package com.ebelter.temperaturegun.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.btcomlib.views.dialogs.picktime2.CustomDatePicker;
import com.ebelter.temperaturegun.R;
import com.ebelter.temperaturegun.TAppConfig;
import com.ebelter.temperaturegun.model.app.TemUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class PeopleChooseActivity extends TBaseActivity {


    public static final int REQUEST_CODE = 12;
    public static final int RESULT_CODE = 33;

    public static final String IsChange = "isChange";

    public static String VISITER_BIR;//访客生日

    private ImageView top_iv;
    private TextView top_tv1;
    private TextView pc_user1_tv;
    private TextView pc_user2_tv;
    private ImageView pc_user1_guo_iv;
    private ImageView pc_user2_guo_iv;


    private int srcUserModel;//0代表用户 1代表访客
    private int selectUserModel;



    @Override
    protected void initView() {
        top_iv=findViewById(R.id.top_iv);
        top_tv1=findViewById(R.id.top_tv1);
        top_tv1.setText(CommonLib.getString(R.string.chy));
        ViewUtils.displayView(top_tv1);

        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });

        pc_user1_tv=findViewById(R.id.pc_user1_tv);
        pc_user2_tv=findViewById(R.id.pc_user2_tv);
        pc_user1_guo_iv=findViewById(R.id.pc_user1_guo_iv);
        pc_user2_guo_iv=findViewById(R.id.pc_user2_guo_iv);


        ViewUtils.setTextViewStr(pc_user1_tv, TemUtils.getUerNickName());

    }

    @Override
    protected boolean isInitMainBaseHandle() {
        return true;
    }

    private void back() {

        mMainBaseHandle.postDelayed(new Runnable() {
            @Override
            public void run() {
                closeActivity();
            }
        },100);

    }

    @Override
    protected void initData() {
        if (TAppConfig.isVisitorModel){
            srcUserModel=1;
        }else {
            srcUserModel=0;
        }
        selectUserModel=srcUserModel;
        updateSelectUserUI(selectUserModel);


    }

    private void updateSelectUserUI(int userModel){
        ViewUtils.hideView(pc_user1_guo_iv);
        ViewUtils.hideView(pc_user2_guo_iv);
        ViewUtils.displayView(userModel==0?pc_user1_guo_iv:pc_user2_guo_iv);

    }

    @Override
    protected int setContentViewId() {
        return R.layout.activity_people_choose;
    }





    private void closeActivity(){


         if (selectUserModel==1){
             //如果此时是访客 就需要判断一下访客有没有填写生日
             if (TextUtils.isEmpty(VISITER_BIR)){
                 ToastUtil.show(R.string.shrbnwk);
                 return;
             }
         }

        //数据是使用Intent返回
        Intent intent = new Intent();
        //把返回数据存入Intent
        intent.putExtra(IsChange, selectUserModel!=srcUserModel);
        //设置返回数据
        this.setResult(RESULT_CODE, intent);

        if (selectUserModel!=srcUserModel){
            TAppConfig.isVisitorModel=!TAppConfig.isVisitorModel;
        }
         finish();
    }

    public void userModelClick(View view) {
        selectUserModel=0;
        updateSelectUserUI(selectUserModel);
        VISITER_BIR=null;
    }

    CustomDatePicker datePicker;
    private String dateStr;
    public void visiterModelClick(View view) {
        selectUserModel=1;
        updateSelectUserUI(selectUserModel);
        VISITER_BIR = null;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String time = sdf.format(new Date());
        String date = time.split(" ")[0];
        if (datePicker==null){

            datePicker = new CustomDatePicker(this,new CustomDatePicker.ResultHandler() {
                @Override
                public void handle(String time) {
                    LogUtils.i(TAG,time);
                    //dateStr = time.substring(0,10);
                    dateStr = time;

                }
            }, "1945-01-01 00:00", time);
            // datePicker.setSelectedCalender("1991-06-13");
            datePicker.showSpecificTime(false); //显示时和分
            datePicker.setIsLoop(false);
            datePicker.setDayIsLoop(true);
            datePicker.setMonIsLoop(true);
        }

        datePicker.show(date);

        if (TextUtils.isEmpty(dateStr)){
            long ago30 = System.currentTimeMillis()-30*365*24*3600*1000l;
            dateStr = TimeUtils.formatTime1(ago30);
        }

        datePicker.setSelectedTime(dateStr);

        datePicker.setBackgroundColor(Color.parseColor("#0176ff"));
        datePicker.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {//内部已经处理了
                VISITER_BIR = null;
            }

            @Override
            public void ok(Dialog dialogView) {
                VISITER_BIR = dateStr;
                //ViewUtils.setTextViewStr(addt_date_tv,dateStr.substring(0,10));

            }
        });
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode==KeyEvent.KEYCODE_BACK){
            back();
        }

        return true;
    }


}
